/*
 * Created on Jun 22, 2005
 */
package dbsucxentW.storageW.parserW;

/**
 * This class is used to group and count the children of each node in the tree
 * based on their element name
 * @author Seah Boon Siew
 */
public class SiblingCount {

    private String name[][]; // an array of name for each level in the tree
    private int count[][]; // the number of elements with the corresponding name in each level
    private int noOfNames[]; // number of names in a level
    private int parentLevel; // current parent level of the counted siblings

    public SiblingCount() {
        name = new String[20][1000];
        count = new int[20][1000];
        noOfNames = new int[20];

        for (int x = 0; x < 20; x++) {
            for (int y = 0; y < 1000; y++) {
                name[x][y] = "";
                count[x][y] = 0;
            }
            noOfNames[x] = 0;
        }
        parentLevel = 0;
    }

    public int add(String nm) {
        //klarinda 20060628 - moved from down (dunno why if the if-block is inside for loop, it is not executed
        if (nm == null) {
            return 0;
        }
        //end of klarinda

        for (int x = 0; x < noOfNames[parentLevel]; x++) {
            if ((name[parentLevel][x]) == null) {
                return 0;
            }

            if ((name[parentLevel][x]).equals(nm)) {
                count[parentLevel][x]++;
                return count[parentLevel][x];
            }

        }

        // new element

        name[parentLevel][noOfNames[parentLevel]] = nm;
        count[parentLevel][noOfNames[parentLevel]] = 0;
        noOfNames[parentLevel]++;
        return 0;
    }

    public void setLevel(int level) {
        if (level < parentLevel) {
            int x;
            for (x = parentLevel; x > level; x--) {
                noOfNames[x] = 0;
            }
        }

        parentLevel = level;
    }
}
